package cn.edu.scnu.controller;

import cn.edu.scnu.config.Result;
import cn.edu.scnu.entity.Member;
import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.entity.MovieDetails;
import cn.edu.scnu.service.MovieService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("/movies")
public class MovieController {

    @Autowired
    private MovieService movieService;

    /**
     * 获取所有电影
     * @param page
     * @param size
     * @param type
     * @param region
     * @return
     */
    @GetMapping("/movies/search")
    public Result searchType(@RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "12") int size,
                             @RequestParam(required = false) String type,
                             @RequestParam(required = false) String region) {
        List<Movie> records = movieService.findMovies(page, size, type, region);
        Map<String, Object> response = new HashMap<>();
        response.put("total", movieService.getMovieCount(type, region));
        response.put("records", records);
        return Result.success(response);
    }

    /**
     * 获取电影详情,分成vip和非vip，顺便完成了访问权限
     * @param movieId
     * @param session
     * @return
     */
    @GetMapping("/movies/play/{movieId}")
    public Result playMovie(@PathVariable String movieId, HttpSession session) {
        Member member = (Member) session.getAttribute("member");
        boolean canWatch = movieService.canWatchMovie(member, movieId);
        if (!canWatch) {
            return Result.error("没有权限观看此电影");
        }
        MovieDetails movieDetails = movieService.getMovieDetails(movieId);
        return Result.success(movieDetails);
    }

}
